<?php

namespace app\api\logic;

use app\common\exception\BusinessException;
use app\common\model\CoursePaper;
use app\common\model\CourseQuestionCategory;

class Paper
{
    /**
     * 试卷列表
     * @param $param
     * @return array
     */
    public function getList($param): array
    {
        // 提取参数
        $courseId = $param['course_id'] ?? 0;
        $categoryId = $param['category_id'] ?? 0;

        // 查询条件
        $where = [['status', '=', 1]];
        if ($courseId) {
            $where[] = ['course_id', '=', $courseId];
        }
        if ($categoryId) {
            $where[] = ['category_id', '=', $categoryId];
        }

        // 查询字段
        $field = 'id,course_id,name,question_count,create_time,update_time';

        // 查询数据
        $list = app(CoursePaper::class)->field($field)->limit(100)->where($where)->order(['sort' => 'desc', 'id' => 'asc'])->select();
        $list = $list ? $list->toArray() : [];

        // 处理数据
        foreach ($list as $key => $item) {
            $list[$key]['max_page'] = ceil($item['question_count'] / 10);
        }

        // 返回
        return $list;
    }

    /**
     * 顶级分类
     * @param $param
     * @return array
     */
    public function categoryList($param): array
    {
        // 提取参数
        $courseId = $param['course_id'];

        // 查询条件
        $where = [
            ['status', '=', 1],
            ['course_id', '=', $courseId],
        ];
        if (isset($param['pid'])) {
            $where[] = ['pid', '=', $param['pid']];
        }
        if (isset($param['level'])) {
            $where[] = ['level', '=', $param['level']];
        }
        if ($courseId) {
            $where[] = ['course_id', '=', $courseId];
        }

        // 检查条件
        if (count($where) == 1) {
            throw new BusinessException('查询条件异常');
        }

        // 查询字段
        $field = 'id,name,desc,tag_id,icon,level,paper_num,question_count';

        // 查询数据
        $list = app(CourseQuestionCategory::class)->field($field)->limit(50)->where($where)->order(['weigh' => 'desc', 'id' => 'asc'])->select();
        $list = $list ? $list->toArray() : [];

        // 处理数据
        foreach ($list as $key => $item) {
            $list[$key]['is_multi_level'] = app(CourseQuestionCategory::class)->isMultiLevel($item['tag_id']);
        }

        // 返回
        return $list;
    }

    /**
     * 顶级分类详情
     * @param $param
     * @return array
     * @throws
     */
    public function categoryDetail($param)
    {
        // 提取参数
        $id = $param['id'];

        // 查询字段
        $field = 'id,name,level,tag_id,paper_num';

        // 查询条件
        $where = [
            ['id', '=', $id],
            ['status', '=', 1],
        ];

        // 查询数据
        $category = app(CourseQuestionCategory::class)->where($where)->field($field)->find();
        if (!$category) {
            throw new BusinessException('分类信息不存在');
        }
        return $category->toArray();
    }

    /**
     * 二级分类
     * @param $param
     * @return array
     */
    public function subCategoryList($param): array
    {
        // 提取参数
        $pid = $param['pid'];

        // 查询条件
        $where = [
            'pid' => $pid,
            'level' => 1,
        ];

        // 查询数据
        $categoryList = app(CourseQuestionCategory::class)->limit(10)->where($where)->order(['weigh' => 'asc', 'id' => 'asc'])->select();
        $categoryList = $categoryList ? $categoryList->toArray() : [];

        // 返回
        return $categoryList;
    }
}
